package process;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import org.junit.Test;

public class Flink_Process_OnTimer {

    public static class MyKeyedProcess extends KeyedProcessFunction<String,String,String>{

        @Override
        public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
            //输出数据
            out.collect(value);

            //注册定时器
            ctx.timerService().registerProcessingTimeTimer(ctx.timerService().currentProcessingTime() + 2000L);
        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
            System.out.println("2s后触发定时器");
        }
    }

    @Test
    public void test() throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);
        env.socketTextStream("node193",9999)
                .flatMap(new FlatMapFunction<String, Tuple2<String,Integer>>() {
                    @Override
                    public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                        for (String s : value.split(" ")) {
                            out.collect(new Tuple2<>(s,1));
                        }
                    }
                })
                .keyBy(value -> value.f0)
                .process(new KeyedProcessFunction<String, Tuple2<String, Integer>, Tuple2<String,Integer>>() {
                    private ValueState<Integer> sum;

                    @Override
                    public void open(Configuration configuration){
                        sum = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("word",Integer.class));
                    }

                    @Override
                    public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                        Integer temp = sum.value();
                        if(temp!=null){
                            sum.update(temp+value.f1);
                        }else{
                            sum.update(value.f1);
                        }
                        out.collect(new Tuple2<>(value.f0,sum.value()));
                        ctx.timerService().registerProcessingTimeTimer(ctx.timerService().currentProcessingTime()+2000L);
                    }

                    @Override
                    public void onTimer(long timestamp,OnTimerContext ctx,Collector<Tuple2<String,Integer>> out){
                        System.out.println("等待2s");
                    }
                })
                .print();
        env.execute();
    }

}
